<template>
  <div>
    <el-dialog
      title="发货信息查询"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="80%"
      @close="close"
    >

      <div class="top">
        <el-form :inline="true" style="margin-top: 10px">
          <el-form-item label="选择状态" class="mt-form-item-left">
            <el-select v-model="dropshipping_state" clearable placeholder="选择状态" size="small">
              <el-option v-for="item in dropshipping_states" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item class="mt-form-item-search">
            <el-input
              v-model="kwInput"
              placeholder="输入关键词进行搜索"
              class="input-with-select"
              clearable
              :autosize="{ minRows: 4}"
              @keyup.native.enter="handleSearch"
            >
              <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </el-input>
          </el-form-item>
        </el-form>
      </div>

      <el-table
        v-loading.fullscreen.lock="listLoading"
        :data="data"
        border
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="handleSort"
      >
        <el-table-column align="center" label="跟踪单号" prop="tracking_no" sortable="custom" />
        <el-table-column align="center" label="下单账号" prop="dropshipping_account" sortable="custom" />
        <el-table-column align="center" label="交易ID" prop="dropshipping_order_id" sortable="custom" />
        <el-table-column align="center" label="订单编号" prop="psorder__id_order" sortable="custom" />
        <el-table-column align="center" label="email" prop="psorder__email" sortable="custom" />
        <el-table-column align="center" label="金额" prop="psorder__total_paid" sortable="custom" />
        <el-table-column align="center" label="create_at" prop="create_at" sortable="custom" />
      </el-table>

      <el-pagination
        v-if="data.length"
        background
        class="mt-pagination"
        :current-page="page"
        :page-sizes="[10,20,50,100]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />
    </el-dialog>
  </div>
</template>

<script>
import { Get } from '@/api/orders/psordershipping'

import qs from 'qs'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Shippingsearch',
  props: ['shippingsearchVisual'],
  data() {
    return {
      dropshipping_states: [
        {
          label: '刚创建',
          value: 0
        },
        {
          label: '已经发货',
          value: 1
        },
        {
          label: '已经到达',
          value: 2
        },
        {
          label: '已经妥投',
          value: 3
        },
        {
          label: '速卖通提醒确认',
          value: 4
        }
      ],
      data: [],
      dialogVisible: false,
      listLoading: true,
      kwInput: '',
      order_by: '',
      dropshipping_state: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0
    }
  },
  watch: {
    shippingsearchVisual(v) {
      this.dialogVisible = v
      if (v) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        pagesize: this.pagesize,
        page: this.page,
        dropshipping_state: this.dropshipping_state,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(err => {
          console.log(err)
          this.listLoading = false
        })
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeMyDialog', false)
    }
  }
}
</script>

<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-input {
    width: auto;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 5px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 5px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 10px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 60px;
    padding-top: 5px;
  }

  .top >>> .el-select {
    width: 300px;
  }
</style>
